package com.qingfeng.medical.biz.auth.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingfeng.medical.biz.auth.domain.excel.SysUserExcelVo;
import com.qingfeng.medical.biz.auth.mq.producer.RabbitSendMsg;
import com.qingfeng.medical.biz.auth.service.SysUserService;
import com.qingfeng.medical.config.mq.RabbitMqConfig;
import lombok.SneakyThrows;

import java.util.Map;

/**
 * @author 清风学Java
 * @version 1.0.0
 * @date 2022/12/23
 */
public class UserVoExcelListener extends AnalysisEventListener<SysUserExcelVo> {

    private SysUserService sysUserService;
    private RabbitSendMsg rabbitSendMsg;
    private ObjectMapper objectMapper;

    public UserVoExcelListener(SysUserService sysUserService,
                               RabbitSendMsg rabbitSendMsg,
                               ObjectMapper objectMapper) {
        this.sysUserService = sysUserService;
        this.rabbitSendMsg = rabbitSendMsg;
        this.objectMapper = objectMapper;
    }

    /**
     * 一行一行读取Excel内容，从第二行读取，第一行是表头不读取
     *
     * @param userReadVo
     * @param analysisContext
     */
    @SneakyThrows
    @Override
    public void invoke(SysUserExcelVo userReadVo, AnalysisContext analysisContext) {
        // 使用MQ去做操作
        rabbitSendMsg.sendMsg(
                objectMapper.writeValueAsString(userReadVo),
                RabbitMqConfig.ROUTINGKEY_USER_INFO
        );
    }


    /**
     * 读取Excel表头第一行的数据
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {

    }

    /**
     * 读取Excel结束后调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
